<template>
  <div class="main-container">
    <el-form :model="query" inline label-width="80px">
      <el-form-item>
        <el-button v-permission="'sys:menu:save'" type="primary" @click="openFormModal(null, '0')">添加菜单</el-button>
      </el-form-item>
      <el-form-item>
        <el-input v-model="query.menuName" placeholder="请输入菜单名称" prefix-icon="Search" clearable />
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery()">刷新</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="page.loading" :data="page.list" :tree-props="{ children: 'childrenList', hasChildren: 'hasChildren' }" row-key="id">
      <el-table-column label="名称" min-width="60" prop="menuName">
        <template #default="scope">
          <span v-if="scope.row.menuIcon"><icon :name="scope.row.menuIcon" class="menu-icon" /> </span>
          &nbsp;{{ scope.row.menuName }}
        </template>
      </el-table-column>
      <el-table-column label="类型" min-width="25">
        <template #default="scope">
          <el-tag v-if="scope.row.menuType === 2 && !scope.row.isShow">路由</el-tag>
          <enum-view v-else :enum-name="'menuType'" :enum-value="scope.row.menuType" />
        </template>
      </el-table-column>
      <el-table-column label="路径/组件" prop="menuUrl">
        <template #default="scope">
          <span v-if="scope.row.menuType === 2">
            <el-tag v-if="scope.row.path">{{ scope.row.path }}</el-tag>
            <br />
            {{ scope.row.component }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="前端权限/后端接口" min-width="100" prop="menuUrl">
        <template #default="scope">
          <el-tag v-if="scope.row.permission" style="margin-bottom: 5px">{{ scope.row.permission }}</el-tag>
          <el-input v-if="scope.row.apis" v-model="scope.row.apis" type="textarea" disabled />
        </template>
      </el-table-column>
      <el-table-column label="排序" min-width="20" prop="sort" />
      <el-table-column label="状态" min-width="20">
        <template #default="scope">
          <enum-view :enum-name="'status'" :enum-value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" :width="220">
        <template #default="scope">
          <el-button v-if="scope.row.menuType !== 3" v-permission="'sys:menu:save'" link type="primary" @click="openFormModal(null, scope.row.id)">新增 </el-button>
          <el-divider direction="vertical" />
          <el-button v-permission="'sys:menu:edit'" link type="primary" @click="openFormModal(scope.row, null)"> 编辑 </el-button>
          <el-divider direction="vertical" />
          <el-dropdown>
            <el-button link type="primary">
              更多操作
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <div v-permission="'sys:menu:edit'" @click="handleStatus(scope.row)">
                  <el-dropdown-item v-if="scope.row.status === 0">启用 </el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.status === 1">禁用 </el-dropdown-item>
                </div>
                <div v-permission="'sys:menu:delete'" @click="handleDelete(scope.row)">
                  <el-dropdown-item> 删除 </el-dropdown-item>
                </div>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <menu-form ref="formRef" @refresh="handlePage" />
</template>

<script setup>
  import useTable from '@/utils/table'
  import { ref } from 'vue'
  import MenuForm from './MenuForm.vue'
  import { systemApi } from '@/api/system'
  import EnumView from '@/components/Enum/View/index.vue'

  // 添加/修改
  const formRef = ref()
  const openFormModal = (item, parentId) => {
    formRef.value.onOpen(item, parentId)
  }

  // 基础功能
  const { page, handlePage, query, resetQuery, handleDelete, handleStatus } = useTable({
    page: systemApi.sysMenuList,
    delete: systemApi.sysMenuDelete,
    status: systemApi.sysMenuUpdateStatus
  })
</script>
